import os.path
import ConfigParser
import re

class ConfigServer:
    def __init__(self, server_host, server_port, server_nick, channel_list):
        self.server_host = server_host
        self.server_port = server_port
        self.server_nick = server_nick
        self.channels = re.split(" ", channel_list)

class Config:
    
    # Returns an array of ConfigServer object.
    def servers(self):
        return self.server_list

    def __init__(self):
        try:
            config_fd = open(os.path.expanduser("~/.bakenekorc"))
        except:
            raise Exception("Failed to open configuration file.")

        config = ConfigParser.ConfigParser()
        config.readfp(config_fd)

        # Parse the server list.
        server_names = re.split(" ", config.get("main", "servers"))

        # Get the information for each servers.
        self.server_list = []
        for i in server_names:
            nick = config.get(i, "nick")
            host = config.get(i, "host")
            port = config.getint(i, "port")
            channel_list = config.get(i, "channels")
            config_server = ConfigServer(host, port, nick, channel_list)
            self.server_list = self.server_list + [config_server]



